import socket
import logs


def sendmsg(text, address, port):
    ADDR = (address, port)
    udpServer = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  # 创建socket对象，走udp通道
    # 设置套接字的选项，允许广播
    udpServer.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    host = socket.gethostname()  # 获取本地主机名
    serverAddr = (host, 8849)
    udpServer.bind(serverAddr)  # 绑定服务端地址
    msg = text.encode('utf-8')
    udpServer.sendto(msg, ADDR)  # 发送数据给客户端
    logs.addLog("send mag success.", address, port, text)
